<?php
namespace App\Services\Classes;

class JisuApi
{
    private $basicURL  = 'https://api.jisuapi.com/';
    private $apiKey    = 'fdd8d369a2427f82';


    private $queryOil        = 'oil/query';
    private $queryVIn        = 'vin/query';
    private $carmaintenance  = 'carmaintenance/query';
    private $jzw             = 'jzw/search';

    private $params   = [];
    public function __construct()
    {
        loadLibrary('curl.class');
        loadLibrary('common');

        $this->params['appkey'] = $this->apiKey;
    }



    /**
     * 油价查询
     *
     * @param string $province
     * @return void
     */
    public function queryOil($province = '四川'){
        return $this->command($this->queryOil , 'GET' , ['province' => $province]);
    }

    /**
     * 车架号
     *
     * @param string $vin
     * @return void
     */
    public function queryVIN($vin = ''){
        return $this->command($this->queryVIn , 'GET' , ['vin'=>$vin]);
    }

    /**
     * Undocumented function
     *
     * @param [type] $frameno
     * @return void
     */
    public function queryCarMaintaince($frameno){
        return $this->command($this->carmaintenance , 'GET' , ['frameno'=>$frameno]);

    }

    /**
     * 脑筋急转弯
     *
     * @param string $key
     * @return void
     */
    public function queryJZW($key = ''){
        return $this->command($this->jzw , 'GET' , ['key'=>$key]);
    }


    /**
     * curl
     *
     * @param [type] $url
     * @param [type] $method
     * @param array $params
     * @return void
     */
    public function command($url , $method , $params = []){
        $curl = new \Curl();
        $curl->setSsl();
        $curl->setTimeout(5, 30);
        $params = array_merge($this->params , $params);
        $url = $this->basicURL . $url ;
        $data = call_user_func([$curl, $method], $url, $params);
        $statusCode = $curl->getHttpCode();
        $curl->close();
        $result = json_decode($data);
        logger('api-request:'.$url,json_decode($data,true));
        return $result;
    }
}
